/**
 * cakeshelf控制器模块
 * controller\cakeshelf.js
 */

const express = require("express");

const util = require("../common/util");
/**
 * @typedef {CakeshelfDB}
 */
const CakeshelfDB = require("../model/sqlite/cakeshelf");

/**
 * 获取书架信息
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function find(req, res) {
    (async function () {
        let db = CakeshelfDB.getInstance();
        await db.connect();
        let result = await db.find(req.params.id, req.session.user.id);
        //await db.close();
        util.logFormat(`获取【${req.params.id}】购物车信息%O`, result);
        res.json(
            util.FormatJSONData(200, `获取购物车信息【${req.params.id}】`, result)
        );
    })();
}

/**
 * 获取书架信息列表
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function findAll(req, res) {
    (async function () {
        //1.处理输入的数据
        //FIXME: 输入数据的正确性校验，一致性校验，完整性校验
        let userId = req.session.user.id;
        let orderBy = req.query.order_by ? req.query.order_by : "id";
        let sort = req.query.sort ? eq.query.sort : "desc";
        let limit = req.query.limit ? req.query.limit : -1;
        let offset = req.query.offset ? req.query.offset : -1;
        //2. 访问数据
        let db = CakeshelfDB.getInstance();
        await db.connect();
        let result = await db.findAll(userId, orderBy, sort, limit, offset);
        //await db.close();

        //3.输出数据
        util.logFormat(`获取指定用户【${userId}】的购物车蛋糕信息%O`, result);
        res.json(util.FormatJSONData(200, `获取用户购物车蛋糕信息列表`, result));
    })();
}

/**
 * 新增书籍信息
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function add(req, res) {
    (async function () {
        //FIXME:req.body的数据安全性校验
        let cakeshelf = {
            userId: req.session.user.id,
            cakeId: req.body.cakeId,
            createdTime: Date.now(),
            updatedTime: Date.now(),
        };
        let db = CakeshelfDB.getInstance();
        await db.connect();
        let result = await db.add(cakeshelf);
        //await db.close();
        util.log(`新增书购物车蛋糕信息：lastID->${result}`);
        res.json(util.FormatJSONData(201, `购物车中添加蛋糕成功`, { lastID: result }));
    })();
}

/**
 * 更新书籍信息
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function update(req, res) {
    (async function () {
        //FIXME:req.body的数据安全性校验
        let cakeshelf = {
            id: req.params.id,
            readStatus: req.body.readStatus,
            ranking: req.body.ranking,
            updatedTime: Date.now(),
        };

        let db = CakeshelfDB.getInstance();
        await db.connect();
        let result = await db.update(cakeshelf);
        //await db.close();
        util.log(`更新购物车中蛋糕相关信息：changes->${result}`);
        res.json(util.FormatJSONData(200, `更新购物车中蛋糕相关信息`, { changes: result }));
    })();
}

/**
 * 删除书籍信息
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function remove(req, res) {
    (async function () {
        //FIXME:数据合法性校验
        let cakeshelfId = req.params.id;
        let db = CakeshelfDB.getInstance();
        await db.connect();
        let result = await db.remove(cakeshelfId);
        //await db.close();
        util.log(`删除购物车中蛋糕信息：changes->${result}`);
        res.json(util.FormatJSONData(204, `删除购物车中蛋糕`, { changes: result }));
    })();
}

/**
 * 获取用户书架书籍总数
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function getCount(req, res) {
    (async function () {
        //FIXME:数据合法性校验
        let userId = req.session.user.id;
        let db = CakeshelfDB.getInstance();
        await db.connect();
        let result = await db.getCount(userId);
        //await db.close();
        util.logFormat("购物车中蛋糕总数：%O", result);
        res.json(util.FormatJSONData(200, `获取购物车中蛋糕总数`, result));
    })();
}

/**
 * 更新阅读状态
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function updateReadStatus(req, res) {
    (async function () {
        //FIXME:数据合法性校验
        let id = req.params.id;
        let readStatus = req.body.readStatus;

        let db = CakeshelfDB.getInstance();
        await db.connect();
        let result = await db.updateReadStatus(id, readStatus);
        //await db.close();
        util.log(`更新书架中书籍阅读状态：${readStatus}`);
        res.json(
            util.FormatJSONData(200, `更新书架中书籍阅读状态`, { changes: result })
        );
    })();
}

/**
 * 更新书架中的书籍评分
 * @param {express.Request} req  请求
 * @param {express.Response} res  响应
 */
function updateRanking(req, res) {
    (async function () {
        //FIXME:数据合法性校验
        let id = req.params.id;
        let ranking = req.body.ranking;

        let db = CakeshelfDB.getInstance();
        await db.connect();
        let result = await db.updateRanking(id, ranking);
        //await db.close();
        util.log(`更新书架中书籍评分：${ranking}`);
        res.json(
            util.FormatJSONData(200, `更新书架中书籍阅读状态`, { changes: result })
        );
    })();
}

module.exports = {
    find,
    findAll,
    add,
    update,
    remove,
    getCount,
    updateReadStatus,
    updateRanking
};